package com.monkeygroup.mygoplus.view.entrance.fragment;

import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.net.ParseException;
import android.net.Uri;
import android.os.Bundle;

import android.provider.MediaStore;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.monkeygroup.mygoplus.R;
import com.monkeygroup.mygoplus.common.AppConfig;
import com.monkeygroup.mygoplus.common.DefinedView.CircleImageView;
import com.monkeygroup.mygoplus.common.DefinedView.MessageChoosePhotoDialog;
import com.monkeygroup.mygoplus.common.DefinedView.MyAlertDialog;
import com.monkeygroup.mygoplus.common.utils.DateUtils;
import com.monkeygroup.mygoplus.common.dateview.JudgeDate;
import com.monkeygroup.mygoplus.common.dateview.ScreenInfo;
import com.monkeygroup.mygoplus.common.dateview.WheelMain;
import com.monkeygroup.mygoplus.common.dateview.WheelView;
import com.monkeygroup.mygoplus.common.utils.FSDateTime;
import com.monkeygroup.mygoplus.common.utils.FormFile;
import com.monkeygroup.mygoplus.common.utils.ImageTools;
import com.monkeygroup.mygoplus.common.utils.LocalInfo;
import com.monkeygroup.mygoplus.common.utils.VilidateUtil;
import com.monkeygroup.mygoplus.common.utils.YFileManager;
import com.monkeygroup.mygoplus.common.utils.YTools;
import com.monkeygroup.mygoplus.common.utils.onViewClick;
import com.monkeygroup.mygoplus.http.HttpModel;
import com.monkeygroup.mygoplus.http.RequestUrl;
import com.monkeygroup.mygoplus.user.vo.PersonalNomalInfo;
import com.monkeygroup.mygoplus.view.OnFragmentResultListener;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by cumt on 2017/3/12.
 */

public class PersonalDataFragment extends Fragment implements View.OnClickListener, OnFragmentResultListener {



    private View mView;

    private Activity activity;
    //头部
    private TextView txtTitle;
    private ImageView imgBack;

    // Fragment管理对象
    private FragmentManager manager;
    private FragmentTransaction ft;

    private WheelMain wheelMainDate;
    private String beginTime;

    private TextView tv_house_time, tv_center, btnNext, txtRegisterNext;
    private Spinner txtYear, txtMonth, txtDay, txtSex;
    private String birthYear = "1900", birthMonth = "01", birthDay = "01", mysex = "0";
    private ArrayAdapter<String> yearAdapter, monthAdapter, dayAdapter;

    private LinearLayout llImg, llAddFriend;
    private EditText userName;
    private ImageView checkboxLogin, imgAdd;

    private String imgPath;
    private SharedPreferences sh;
    private static final int YEAR = 0, MONTH = 1, DAY = 2;
    private final String TAG = "personData";

    //請求參數
    private Map<String, String> params;
    private HttpModel httpModel;
    private HttpModel upload;

    private FormFile files;
    private File file;

    private ArrayAdapter<String> sexAdapter;
    //數據
    private PersonalNomalInfo vo;
    private String birthday;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        activity = this.getActivity();
        manager = getFragmentManager();

        sh = YTools.getSharePreferences(activity, "camera");

        if (mView == null) {
            mView = inflater.inflate(R.layout.personal_data, container, false);

            initView();
            initListener();
            initRequest();
        }
        return mView;
    }

    private void initView() {

        txtTitle = (TextView) mView.findViewById(R.id.txt_title);
        txtTitle.setText("設定個人資料");
        imgBack = (ImageView) mView.findViewById(R.id.img_back);


        tv_house_time = (TextView) mView.findViewById(R.id.tv_house_time);
        tv_center = (TextView) mView.findViewById(R.id.tv_center);
        llImg = (LinearLayout) mView.findViewById(R.id.ll_myimg);
        llAddFriend = (LinearLayout) mView.findViewById(R.id.ll_add_friend);
        userName = (EditText) mView.findViewById(R.id.user_name);
        btnNext = (TextView) mView.findViewById(R.id.btn_next);
        txtRegisterNext = (TextView) mView.findViewById(R.id.txt_register_next);
        checkboxLogin = (ImageView) mView.findViewById(R.id.checkbox_login);
        imgAdd = (ImageView) mView.findViewById(R.id.img_add);

        txtYear = (Spinner) mView.findViewById(R.id.txt_year);
        txtMonth = (Spinner) mView.findViewById(R.id.txt_month);
        txtDay = (Spinner) mView.findViewById(R.id.txt_day);
        txtSex = (Spinner) mView.findViewById(R.id.txt_sex);

        params = new HashMap<String, String>();

        if (LocalInfo.getUserInfo(activity) != null) {

            vo = LocalInfo.getUserInfo(activity);
            Log.e("getNamegetName===", vo.getName());
        } else {
            vo = new PersonalNomalInfo();
        }
        vo.setGender("1");
    }

    private void initListener() {

        mView.findViewById(R.id.btn_next).setOnClickListener(this);
        mView.findViewById(R.id.ll_year).setOnClickListener(this);
        mView.findViewById(R.id.txt_register_next).setOnClickListener(this);
        mView.findViewById(R.id.ll_add_friend).setOnClickListener(this);
        mView.findViewById(R.id.ll_myimg).setOnClickListener(this);
        tv_house_time.setOnClickListener(this);

        String[] items = getResources().getStringArray(R.array.sexarr);
        sexAdapter = new ArrayAdapter<String>(activity, android.R.layout.simple_spinner_item, items);
        sexAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        txtSex.setAdapter(sexAdapter);

        setBirthDayData();

        imgBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getFragmentManager().popBackStack();
            }
        });

        userName.addTextChangedListener(new TextWatcher() {


            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!TextUtils.isEmpty(s)) {
                    btnNext.setBackgroundResource(R.drawable.btn_login_selector);

                } else {
                    btnNext.setBackgroundResource(R.drawable.btn_grey_shape);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });


        txtSex.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                Log.e("ArrayAdadapter===", "" + sexAdapter.getItem(position));

                if (position == 0) {
                    vo.setGender(String.valueOf(1));

                } else if (position == 1) {
                    vo.setGender(String.valueOf(0));

                }

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }


        });

    }

    private void initRequest() {

        //註冊請求
        httpModel = new HttpModel(RequestUrl.baseUrl + RequestUrl.registerUrl, params, new HttpModel.SimpleOnHttpRequestListener() {
            @Override
            public void onRequestFinished(int resultCode, String resultMsg, Object data) {

                if (!VilidateUtil.isSuccess(resultCode)) {

                    MyAlertDialog dialog = new MyAlertDialog(activity, resultMsg, "");
                    dialog.show(activity, resultMsg, 1);


                } else {

                    RegisterCompletefragment register = new RegisterCompletefragment();
                    ft = manager.beginTransaction();
                    //当前的fragment会被RegisterFragment替换
                    ft.replace(R.id.fragment_container, register);
                    ft.addToBackStack(null);
                    ft.commit();

                }
            }
        }, true);

    }

    private java.text.DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    public void showBottoPopupWindow() {
        WindowManager manager = (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);
        Display defaultDisplay = manager.getDefaultDisplay();
        DisplayMetrics outMetrics = new DisplayMetrics();
        defaultDisplay.getMetrics(outMetrics);
        int width = outMetrics.widthPixels;
        View menuView = LayoutInflater.from(activity).inflate(R.layout.show_popup_window, null);
        final PopupWindow mPopupWindow = new PopupWindow(menuView, (int) (width * 0.8),
                ActionBar.LayoutParams.WRAP_CONTENT);
        ScreenInfo screenInfoDate = new ScreenInfo(activity);

        wheelMainDate = new WheelMain(menuView, true);
        wheelMainDate.screenheight = screenInfoDate.getHeight();
        String time = DateUtils.currentMonth().toString();
        Calendar calendar = Calendar.getInstance();
        if (JudgeDate.isDate(time, "yyyy-MM-DD")) {
            try {
                calendar.setTime(new Date(time));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int hours = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        wheelMainDate.initDateTimePicker(year, month, day, hours, minute);
        final String currentTime = wheelMainDate.getTime().toString();
        mPopupWindow.setAnimationStyle(R.style.AnimationPreview);
        mPopupWindow.setTouchable(true);
        mPopupWindow.setFocusable(true);
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
        mPopupWindow.showAtLocation(tv_center, Gravity.CENTER, 0, 0);
        mPopupWindow.setOnDismissListener(new poponDismissListener());
        backgroundAlpha(0.6f);
        TextView tv_cancle = (TextView) menuView.findViewById(R.id.tv_cancle);
        TextView tv_ensure = (TextView) menuView.findViewById(R.id.tv_ensure);
        TextView tv_pop_title = (TextView) menuView.findViewById(R.id.tv_pop_title);
        final WheelView viewyear = (WheelView) menuView.findViewById(R.id.year);

        tv_pop_title.setText("选择起始时间");
        tv_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                mPopupWindow.dismiss();
                backgroundAlpha(1f);
            }
        });
        tv_ensure.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                beginTime = wheelMainDate.getTime().toString();
                try {
                    Date begin = dateFormat.parse(currentTime);
                    Date end = dateFormat.parse(beginTime);
                    tv_house_time.setText(DateUtils.currentTimeDeatil(begin));
                } catch (ParseException e) {
                    e.printStackTrace();
                } catch (java.text.ParseException e) {
                    e.printStackTrace();
                }
                mPopupWindow.dismiss();
                backgroundAlpha(1f);
            }
        });

//        viewyear.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Toast.makeText(activity, "view内部设置OnClickListener"+viewyear.getId()+"==="+viewyear.getVisibleItems(), Toast.LENGTH_SHORT).show();
//
//            }
//        });

//        viewyear.setOnViewClick(new onViewClick() {
//            @Override
//            public void onClick(float scrollX, float scrollY) {
//                Toast.makeText(activity, "view内部设置OnClickListener"+viewyear.getCurrentItem(), Toast.LENGTH_SHORT).show();
//            }
//        });

    }

    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        activity.getWindow().setAttributes(lp);
    }

    @Override
    public void OnFragmentResult(int requestCode, int resultCode, Intent data) {
        Log.e("mygo", "code222=====" + requestCode);
    }


    class poponDismissListener implements PopupWindow.OnDismissListener {
        @Override
        public void onDismiss() {
            backgroundAlpha(1f);
        }

    }

    private void setBirthDayData() {
        yearAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_spinner_item, getSpinnerValue(2100, YEAR));
        yearAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        txtYear.setAdapter(yearAdapter);


        txtYear.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                Log.d(TAG, "onItemSelected birthYear");
                birthYear = yearAdapter.getItem(pos);
                displayMonthSpinner();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // Another interface callback
            }
        });

    }

    private void displayMonthSpinner() {
        monthAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_spinner_item, getSpinnerValue(12, MONTH));
        monthAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        txtMonth.setAdapter(monthAdapter);

        txtMonth.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                Log.d(TAG, "onItemSelected birthMonth");
                birthMonth = monthAdapter.getItem(pos);
                dynamicDisplayDaySpinner();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // Another interface callback
            }
        });
    }

    private void dynamicDisplayDaySpinner() {
        int currYear = Integer.valueOf(birthYear);
        int currMonth = Integer.valueOf(birthMonth);
        dayAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_spinner_item, getSpinnerValue(getMaxDay(currYear, currMonth), DAY));
        dayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        txtDay.setAdapter(dayAdapter);

        txtDay.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                Log.d(TAG, "onItemSelected birthDay");
                birthDay = dayAdapter.getItem(pos);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // Another interface callback
            }
        });
    }

    private int getMaxDay(int year, int month) {
        int num;

        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                num = 31;
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                num = 30;
                break;
            case 2:
            default:
                if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
                    num = 29;
                } else {
                    num = 28;
                }
                break;

        }

        return num;
    }

    private String[] getSpinnerValue(int maxValue, int type) {
        int minValue;

        if (type == YEAR) {
            minValue = 1900;
        } else {
            minValue = 1;
        }

        List<String> list = new ArrayList<>();
        for (int i = minValue; i < maxValue + 1; i++) {
            String tempStr;
            if (i < 10) {
                tempStr = "0" + i;
            } else {
                tempStr = String.valueOf(i);
            }
            list.add(tempStr);
        }

        int size = list.size();

        return list.toArray(new String[size]);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.btn_next:

//                boolean isnext = btnNext.getBackground().getCurrent().getConstantState().equals(getResources().getDrawable(R.drawable.btn_login_selector).getCurrent().getConstantState());
                //  Log.e("isnext====",""+isnext);
                vo.setUserName(String.valueOf(userName.getText()));

                vo.setBirthday(birthYear + "-" + birthMonth + "-" + birthDay);

                LocalInfo.saveUserInfo(activity, vo);

                params.put("name", vo.getName());
                params.put("username", vo.getUserName());
                Log.e("vo.getName()===", "" + vo.getName() + ":::" + vo.getMobile());
                params.put("birthday", vo.getBirthday());
                params.put("password", vo.getPassword());
                params.put("mobile", vo.getMobile());
                params.put("gender", vo.getGender());
                params.put("mobile", vo.getMobile());

                if (!"".equals(userName.getText())) {

                    httpModel.startRequest();
                }


                break;
            case R.id.tv_house_time:

                break;
            case R.id.ll_year:

              //  showBottoPopupWindow();

                break;
            case R.id.ll_add_friend:

                boolean isread = checkboxLogin.getDrawable().getCurrent().getConstantState().equals(getResources().getDrawable(R.mipmap.checkbox_login_ok_nor).getCurrent().getConstantState());

                if (isread) {
                    checkboxLogin.setImageDrawable(getResources().getDrawable(R.mipmap.checkbox_login_ok_press));
                } else {
                    checkboxLogin.setImageDrawable(getResources().getDrawable(R.mipmap.checkbox_login_ok_nor));
                }
                break;

            case R.id.txt_register_next:

                txtRegisterNext.setTextColor(getResources().getColor(R.color.forget_font));
//                MainActivity.gotoActivity(activity);
                vo.setUserName(String.valueOf(userName.getText()));

                vo.setBirthday(birthYear + "-" + birthMonth + "-" + birthDay);

                LocalInfo.saveUserInfo(activity, vo);

                params.put("name", vo.getName());
                params.put("username", vo.getUserName());
                Log.e("vo.getName()===", "" + vo.getName() + ":::" + vo.getMobile());
                params.put("birthday", vo.getBirthday());
                params.put("password", vo.getPassword());
                params.put("mobile", vo.getMobile());
                params.put("gender", vo.getGender());
                params.put("mobile", vo.getMobile());

                if (!"".equals(userName.getText())) {

                    httpModel.startRequest();
                }

                break;
            case R.id.ll_myimg:
                chooseImg();
                break;

        }

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.e("mygo", "code11=====" + requestCode);

        if (requestCode == AppConfig.CAMERA_CODE) {
            Log.e("mygo", "照相机返回正常!data=" + data);
            if (data != null) {


                // 如果有返回结果
                Uri mImageCaptureUri = data.getData();
                Bitmap image = null;

                // 如果返回路径不为空
                if (mImageCaptureUri != null) {
                    try {
                        // 取图片
                        image = MediaStore.Images.Media.getBitmap(activity.getContentResolver(), mImageCaptureUri);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    Log.e("mygo", "照相机返回路径为空！");
                }

                File newPic = null;
                try {
                    newPic = YFileManager.getLeaveMsgPicsCacheFile(System.currentTimeMillis() + ".jpg");
                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(activity, "拍照失败，无法读写手机存储卡", Toast.LENGTH_SHORT).show();

                    return;
                }

                imgAdd.setImageURI(Uri.fromFile(newPic));
            } else {
                imgPath = sh.getString("path", "");
                Log.e("mygo", "照相机返回正常!imgPath=" + imgPath);
                if (imgPath.equals("") == false) {


                    File newPic = null;
                    try {
                        newPic = YFileManager.getLeaveMsgPicsCacheFile(System.currentTimeMillis() + ".jpg");

                        File fileFromAlbum = YFileManager.getLeaveMsgPicsCacheFile(imgPath);
                        // final ImageView oneTempImag = new ImageView(activity);
                        CircleImageView oneTempImag = new CircleImageView(activity);
                        if (fileFromAlbum == null
                                || fileFromAlbum.exists() == false) {
                            Toast.makeText(activity, "拍照失败，请重试",
                                    Toast.LENGTH_SHORT).show();
                            return;
                        }
//
                        //                     imgAdd.setImageURI(Uri.fromFile(newPic));
                        if (ImageTools.zoomPic(activity, fileFromAlbum, newPic)) {
//
                            //     imgAdd.setImageURI(Uri.fromFile(newPic));
                            oneTempImag.setImageURI(Uri.fromFile(newPic));
//                            Bitmap bmp = MediaStore.Images.Media.getBitmap(activity.getContentResolver(), Uri.fromFile(newPic));
//                            oneTempImag.setImageBitmap(bmp);
                        } else {
                            // imgAdd.setImageURI(Uri.fromFile(newPic));
                            oneTempImag.setImageURI(Uri.fromFile(fileFromAlbum));
                        }

                        oneTempImag.setScaleType(ImageView.ScaleType.CENTER_CROP);
                        oneTempImag.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));


                        fillFile(String.valueOf(newPic.getPath()));
                        //
                        llImg.removeAllViews();
                        llImg.addView(oneTempImag);

                        imgAdd.setOnLongClickListener(new View.OnLongClickListener() {

                            @Override
                            public boolean onLongClick(View v) {
                                // TODO Auto-generated method stub

                                //上传上传痛点封面

                                return true;
                            }
                        });

                        Log.e("gezi", "获取到图片:" + newPic.getPath());
                    } catch (Exception e) {
                        e.printStackTrace();
                        Toast.makeText(activity, "拍照失败，无法读写手机存储卡",
                                Toast.LENGTH_SHORT).show();
                        return;
                    }
                } else {
                    Toast.makeText(activity, "拍照失败，请重试..",
                            Toast.LENGTH_SHORT).show();
                }
            }
        } else {
            Log.e("mygo", "照相机返回错误！");
        }


        if (requestCode == AppConfig.ALBUM) {
            Log.e("data.getData()",""+data.getData());
            // 来源于相册
            Uri imageFilePath = data.getData();
            File source = YTools.getImageFromSys(activity, imageFilePath);
            if (source == null || source.exists() == false) {
                return;
            }
            try {
                File toFile = YFileManager.getLeaveMsgPicsCacheFile(System
                        .currentTimeMillis() + "_small.jpg");

                //   imgAdd.setImageURI(Uri.fromFile(toFile));
                //     final ImageView oneTempImag = new ImageView(activity);
                CircleImageView oneTempImag = new CircleImageView(activity);

                if (ImageTools.zoomPic(activity, source, toFile)) {
                    oneTempImag.setImageURI(Uri.fromFile(toFile));
                } else {
                    oneTempImag.setImageURI(Uri.fromFile(source));
                }
                oneTempImag.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                oneTempImag.setScaleType(ImageView.ScaleType.CENTER_CROP);

                llImg.removeAllViews();
                llImg.addView(oneTempImag);
                //上传半身像
                fillFile(source.getPath());
                imgAdd.setOnLongClickListener(new View.OnLongClickListener() {

                    @Override
                    public boolean onLongClick(View v) {
                        // TODO Auto-generated method stub
                        //


                        return true;
                    }
                });


            } catch (Exception e) {
                e.printStackTrace();
                Toast.makeText(activity, "存储卡空间不足或者无法读写手机存储卡",
                        Toast.LENGTH_SHORT).show();

            }
        }


    }

    //选择图片来源
    private void chooseImg() {

        MessageChoosePhotoDialog.show(activity, "提示", "选择图片来源:", "本地相册",
                "现场拍照", new MessageChoosePhotoDialog.OnButtonClickListener() {

                    @Override
                    public void onYesClick(TextView txtMessageText) {
                        // TODO Auto-generated method stub
                        // 调用相机

                        imgPath = FSDateTime.getDateTimeShort()
                                + ".jpg";

                        SharedPreferences.Editor edit = sh.edit();
                        edit.putString("path", imgPath);
                        edit.commit();

                        try {
//                            YTools.showCamera(activity, imgPath);
                            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

                            // 下面这句指定调用相机拍照后的照片存储的路径
                            File filePath = YFileManager.getLeaveMsgPicsCacheFile(imgPath);

                            if (null != filePath) {
                                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(filePath));
                                intent.putExtra("type", "type");
                                Log.e("mygo", "准备照相......");
                                startActivityForResult(intent, AppConfig.CAMERA_CODE);
                                Log.e("mygo", "等待......");
                            } else {
                                return;
                            }

                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                            Toast.makeText(activity, "无法读取手机存储卡",
                                    Toast.LENGTH_SHORT).show();
                            //VCRAFunction.addErr(activity, e);
                        }
                    }


                    @Override
                    public void onNoClick() {
                        // TODO Auto-generated method stub
                        // YTools.showPicker(activity);
                        Intent intent = new Intent();
                        intent.setType("image/*");
                        intent.setAction(Intent.ACTION_PICK);
                        startActivityForResult(intent, AppConfig.ALBUM);
                    }
                }, true);
    }


    private void fillFile(String filePath) {
        Log.e("filePathfilePath==",filePath);
        params.clear();
        params.put("mobile", "18580568258");

        try {
            file = new File(filePath);
        } catch (Exception e) {
            e.printStackTrace();
        }

        //上傳圖片請求
        upload = new HttpModel(RequestUrl.baseUrl + RequestUrl.uploadImg, params, new HttpModel.SimpleOnHttpRequestListener() {
            @Override
            public void onRequestFinished(int resultCode, String resultMsg, Object data) {

                Log.e("上傳圖片=====", resultMsg);

            }
        }, "files", file);

        upload.uploadFile();
    }

}
